package com.ktgj.service.service.impl;


import com.ktgj.common.core.utils.DateUtils;
import com.ktgj.service.domain.ContractLinkBoarding;
import com.ktgj.service.domain.ContractLinkBoardingPrice;
import com.ktgj.service.domain.Vipboarding;
import com.ktgj.service.mapper.ContractLinkBoardingMapper;
import com.ktgj.service.mapper.ContractLinkBoardingPriceMapper;
import com.ktgj.service.mapper.VipboardingMapper;
import com.ktgj.service.service.IVipboardingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 值机服务
Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-09-03
 */
@Service
@Transactional
public class VipboardingServiceImpl implements IVipboardingService
{
    @Autowired
    private VipboardingMapper vipboardingMapper;

    @Autowired
    private ContractLinkBoardingMapper contractLinkBoardingMapper;

    @Autowired
    private ContractLinkBoardingPriceMapper contractLinkBoardingPriceMapper;

    /**
     * 查询值机服务

     * 
     * @param boardingId 值机服务
ID
     * @return 值机服务

     */
    @Override
    public Vipboarding selectVipboardingById(Long boardingId)
    {
        return vipboardingMapper.selectVipboardingById(boardingId);
    }

    /**
     * 查询值机服务
列表
     * 
     * @param vipboarding 值机服务

     * @return 值机服务

     */
    @Override
    public List<Vipboarding> selectVipboardingList(Vipboarding vipboarding)
    {
        return vipboardingMapper.selectVipboardingList(vipboarding);
    }

    /**
     * 新增值机服务

     * 
     * @param vipboarding 值机服务
     *                              新增值机
     * @return 结果
     */
    @Override
    public int insertVipboarding(Vipboarding vipboarding)
    {
        vipboarding.setCreateTime(DateUtils.getNowDate());
        return vipboardingMapper.insertVipboarding(vipboarding);
    }

    /**
     * 修改值机服务

     * 
     * @param vipboarding 值机服务

     * @return 结果
     */
    @Override
    public int updateVipboarding(Vipboarding vipboarding)
    {
        vipboarding.setUpdateTime(DateUtils.getNowDate());
        return vipboardingMapper.updateVipboarding(vipboarding);
    }

    /**
     * 批量删除值机服务

     * 
     * @param boardingIds 需要删除的值机服务
ID
     * @return 结果
     */
    @Override
    public int deleteVipboardingByIds(Long[] boardingIds)
    {
        return vipboardingMapper.deleteVipboardingByIds(boardingIds);
    }

    /**
     * 删除值机服务
信息
     * 
     * @param boardingId 值机服务
ID
     * @return 结果
     */
    @Override
    public int deleteVipboardingById(Long boardingId)
    {
        return vipboardingMapper.deleteVipboardingById(boardingId);
    }

    /**
     * 值机与合同多对多查询
     *
     * @param contractId 合同ID
     * @return 值机与合同集合
     * */
    @Override
    public List<Vipboarding> selectVipboardingByContractId (Long contractId) {
        return vipboardingMapper.selectVipboardingByContractId(contractId);
    }

    /**
     * 根据合同id、值机id查询是否存在该数据
     * @param contractId 合同id
     * @param boardingId 值机id
     * @return
     */
    @Override
    public int selectBoardingContract(Long contractId, Long boardingId) {
        return vipboardingMapper.selectBoardingContract(contractId,boardingId);
    }

    /**
     *
     * @param boardingPrice
     * @return
     */
    @Override
    public boolean addBoardingPrice(ContractLinkBoardingPrice boardingPrice) {
        boolean flag = false;
        Long contractboardingId = 0L;
        List<ContractLinkBoarding> linkBoardingList = boardingPrice.getLinkBoardingList();
        for (ContractLinkBoarding boarding : linkBoardingList) {
            ContractLinkBoarding linkBoarding = new ContractLinkBoarding();

            linkBoarding.setContractId(boarding.getContractId());
            linkBoarding.setBoardingId(boarding.getBoardingId());
            linkBoarding.setStatus(boarding.getStatus());
            linkBoarding.setOpemployeeId(boarding.getOpemployeeId());
            linkBoarding.setCreateTime(DateUtils.getNowDate());
            linkBoarding.setUpdateTime(boarding.getUpdateTime());
            int result= contractLinkBoardingMapper.insertContractLinkBoarding(linkBoarding);
            if( result > 0){
                 contractboardingId = linkBoarding.getContractboardingId();
                 break;
            }

        }
        //设置价格表的服务id
        boardingPrice.setContractboardingId(contractboardingId);
        boardingPrice.setCreateTime(DateUtils.getNowDate());
        //执行保存价格操作
        int result =contractLinkBoardingPriceMapper.insertContractLinkBoardingPrice(boardingPrice);
        if(result >0 ){
            flag = true;
        }
        return flag;
    }

    /**
     *
     * @param contractId
     * @return
     */
    @Override
    public List<Vipboarding> selectVipboardingAndContractList(Long contractId){
        List<Vipboarding> vipboardingList=vipboardingMapper.selectVipboardingByContractId(contractId);
        for (Vipboarding vipboarding:vipboardingList){
            Long vipboardingLong=vipboarding.getContractLinkBoarding().getContractboardingId();
            vipboarding.getContractLinkBoardingPrices().addAll(contractLinkBoardingPriceMapper.selectContractLinkBoardingPriceByContractBoardingid(vipboardingLong));
        }
        return vipboardingList;
    }

    /**
     * 值机  机场支持航司服务一对多查询
     * @param boardingId 贵宾厅信息ID
     * @return 值机  机场支持航司服务一对多查询集合
     * */
    @Override
    public List<Vipboarding> selectVipboardingByBoardingId(Long boardingId) {
        return vipboardingMapper.selectVipboardingByBoardingId(boardingId);
    }

    @Override
    public List<Vipboarding> getVipboardingList() {
        return vipboardingMapper.getAll();
    }
}
